
// 定义一个枚举，用来表示不同的退休年龄类型
enum RetirementType {
    // 这里为每个变体指定了一个名称，可以根据需要自定义这些名称
    OriginalRetirementAge55Female,
    OriginalRetirementAge50Female,
    MaleEmployee,
}

impl RetirementType {
    // 用来从字符串中解析出对应的 RetirementType
    fn from_str(s: &str) -> Option<RetirementType> {
        match s {
            "原法定退休年龄55周岁女职工" => Some(RetirementType::OriginalRetirementAge55Female),
            "原法定退休年龄50周岁女职工" => Some(RetirementType::OriginalRetirementAge50Female),
            "男职工" => Some(RetirementType::MaleEmployee),
            _ => None, // 如果匹配不到则返回 None
        }
    }
    // 原定退休年份
    fn origin_retire_year(&self, birth_year: i32) -> i32 {
        match self {
            RetirementType::OriginalRetirementAge55Female => birth_year + 55,
            RetirementType::OriginalRetirementAge50Female => birth_year + 50,
            RetirementType::MaleEmployee => birth_year + 60,            
        }
    }
    fn origin_retire_age(&self) -> i32 {
        match self {
            RetirementType::OriginalRetirementAge55Female => 55,
            RetirementType::OriginalRetirementAge50Female => 50,
            RetirementType::MaleEmployee => 60,            
        }
    }
    // 延迟退休月数
    fn delay_month(&self, birth_year: i32, birth_month: i32) -> i32 {
        let origin_retire_year = self.origin_retire_year(birth_year);
        let diff = months_difference(origin_retire_year, birth_month, 2025, 1);
        if diff >= 0 {
            match self {
                RetirementType::OriginalRetirementAge55Female | RetirementType::MaleEmployee => (diff / 4 + 1).min(36),
                RetirementType::OriginalRetirementAge50Female => (diff / 2 + 1).min(60), 
            }
        } else {
            0
        }
    }
}

// 返回 date1-date2 的月份数
fn months_difference(date1_year: i32, date1_month: i32, date2_year: i32, date2_month: i32) -> i32 {
    // 计算年份和月份之间的差异
    let year_diff = date1_year - date2_year;
    let month_diff = date1_month - date2_month;
    // 返回差异的月份数
    year_diff * 12 + month_diff
}



pub fn retire_time(time: &str, tp: &str) -> String {
    let birth = time.split("-").map(|v| v.parse::<i32>().unwrap()).collect::<Vec<i32>>();
    let birth_year = birth[0];
    let birth_month = birth[1];
    let retire_type = RetirementType::from_str(tp).unwrap();

    // 计算延迟退休月数
    let delay = retire_type.delay_month(birth_year, birth_month);

    // 计算延迟退休时间
    let mut retire_year = retire_type.origin_retire_year(birth_year) + delay / 12;
    let mut retire_month = birth_month + delay % 12;
    if retire_month > 12 {
        retire_year += 1;
        retire_month -= 12;
    }

    // 计算延迟退休年龄
    let retire_age = retire_type.origin_retire_age() as f64 + delay as f64 / 12.0;

    vec![retire_year.to_string()+"-"+&format!("{:02}", retire_month), format!("{:.2}", retire_age).trim_end_matches("0").trim_end_matches(".").to_string(), delay.to_string()].join(",")
}
